package com.yupi.usercenter.controller;

import com.yupi.usercenter.common.BaseResponse;
import com.yupi.usercenter.common.ErrorCode;
import com.yupi.usercenter.common.ResultUtils;
import com.yupi.usercenter.constant.UserConstant;
import com.yupi.usercenter.exception.BusinessException;
import com.yupi.usercenter.model.User;
import com.yupi.usercenter.model.requset.UserDeleteRequest;
import com.yupi.usercenter.model.requset.UserLoginRequest;
import com.yupi.usercenter.model.requset.UserRegisterRequest;
import com.yupi.usercenter.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.net.http.HttpRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin(value = "http://47.121.130.221",allowCredentials = "true")
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @PostMapping("/register")
    BaseResponse<Integer> userRegisger(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
             throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        String userAcoount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        String planetCode = userRegisterRequest.getPlanetCode();
        //控制层主要对参数进行判断 业务层对与业务逻辑有关的进行判断 二者出发点不一样
        if (StringUtils.isAllBlank(userAcoount, userPassword, checkPassword)) {
            throw new BusinessException(ErrorCode.PARAM_ERROR,"数据不完整");
        }
        int i = userService.userRegisger(userAcoount, userPassword, checkPassword, planetCode);
        return ResultUtils.success(i);
    }

    @PostMapping("/login")
    BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"request为空");
        }
        String userAcoount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAllBlank(userAcoount, userPassword)) {
            throw new BusinessException(ErrorCode.PARAM_ERROR,"用户名或密码为空");
        }
        User user = userService.userLogin(userAcoount, userPassword, request);
        return ResultUtils.success(user);
    }
    @PostMapping("/logout")
    BaseResponse<Integer> userLogout( HttpServletRequest request) {
     if (request == null)
     {
         throw new BusinessException(ErrorCode.SYSTEM_ERROR,"request为空");
     }
     userService.userLogout(request);
     return ResultUtils.success(1);
    }

    @GetMapping("/search")
    public BaseResponse<List<User>>userSearch(String username, HttpServletRequest httpServletRequest) {
        Object attribute = httpServletRequest.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        User user = (User) attribute;
        if (user == null)    throw new BusinessException(ErrorCode.NOT_LOGIN,"用户未登录");
        if (!isAdmin(user)) throw new BusinessException(ErrorCode.NO_AUTH,"没有权限查询");

        List<User> users = userService.userSerach(username);
        return ResultUtils.success(users);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean>userDelete(@RequestBody UserDeleteRequest userDeleteRequest, HttpServletRequest httpServletRequest) {
        Object attribute = httpServletRequest.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        User user = (User) attribute;
        if (userDeleteRequest.getId() < 0 )
        {
            throw new BusinessException(ErrorCode.PARAM_ERROR,"id小于0");
        }
        if (!isAdmin(user)) {
            throw new BusinessException(ErrorCode.NO_AUTH,"没有权限删除");
        }
        boolean b = userService.userDelete(userDeleteRequest.getId());
        return ResultUtils.success(b);
    }

    @GetMapping("/current")
    public BaseResponse<User> getCurrent(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object attribute = session.getAttribute(UserConstant.USER_LOGIN_STATE);

        User user = (User) attribute;
        if (user == null)     throw new BusinessException(ErrorCode.SYSTEM_ERROR,"用户未登录");
        User cur = userService.getById(user.getId());
        User safety = userService.getSafety(cur);
        return ResultUtils.success(safety);

    }
//    @PostMapping("/update")
//    public BaseResponse<Boolean> update(HttpServletRequest request)
//    {
//        HttpSession session = request.getSession();
//        Object attribute = session.getAttribute(UserConstant.USER_LOGIN_STATE);
//
//        User user = (User) attribute;
//        if (user == null)     throw new BusinessException(ErrorCode.SYSTEM_ERROR,"用户未登录");
//        if (!isAdmin(user))   throw new BusinessException(ErrorCode.NO_AUTH,"没有权限修改");
//        else return null;
//        return null;
//    }

    public boolean isAdmin(User user) {
        if (user.getUserRole() == 1) {
            return true;
        }
        return false;
    }

}
